var num = 0;
var isok = 0;
$(function() {
    // 初始化加载所有班级数据
    readyclass2();
    $("#save").click(function() {
        $.ajax({
            url: "/blackhouse/class_infomation_servlet",
            type: "post",
            data: { "type": 2, "className": $("#addgrade").val(),"gradeid":$("#class-select"+num).val() },
            dataType: "json",
            success: function(e) {
                if (e == 1) {
                    alert("保存成功");
                    isok=0;
                    readyclass2();
                } else if (e == 0)
                    alert("保存失败");
                else if (e == -1)
                    alert("服务器故障");
            }
        });
    });
    $("#save-1").click(function() {
    	if(isok==0){
    		num++;
        var html = "";
        html += "<tr>";
        html += "<td><input type='checkbox'/></td>";
        html += "<td><select class=\"form-control\" id='class-select"+num+"'></select></td>";
        html += "<td></td>";
        html += "<td><input id=\"addgrade\" type='text'style='outline: none;border: none;'></td>";
        html += "<td></td>";
        html += "<td></td>";
        html += "<td><button type=\"button\" class=\"btn btn-success-1\" name=\"delete\">删除</button></td>";
        html += "</tr>";
        $("#tbody").append(html);
        readygradeindex("class-select"+num);
    	}else{
    		alert("请保存上一个请求");
    	}
    	isok++;
    })

});

function readyclass2() {
    $.ajax({
        url: "/blackhouse/class_infomation_servlet",
        type: "post",
        data: {
            "type": 1
        },
        dataType: "json",
        success: function(e) {
            var html = "";
            for (var i = 0; i < e.length; i++) {
                html += "<tr name=\"" + e[i].id + "\">";
                html += "<td><input type='checkbox'/ ></td>";
                html += "<td>"	+e[i].gradename+"</td>";
                html += "<td>" + e[i].classCode + "</td>";
                html += "<td>" + e[i].className + "</td>";
                html += "<td>" + e[i].classMessage + "</td>";
                html += "<td>" + e[i].createTime + "</td>";
                html += "<td><button type=\"button\" class=\"btn btn-success-1\" name=\"delete\">删除</button></td>";
                html += "</tr>";
            }
            $("#tbody").html(html);
            	num++;
            $("#tbody button").click(function() {
                var id = $(this).parent().parent().attr('name');
                if ($(this).attr("name") == "delete") {
                    Delete(id);
                }
            });
        }
    });
}
//删除年级信息
function Delete(id) {
    $.ajax({
        url: "/blackhouse/class_infomation_servlet",
        type: "post",
        data: {
            "type": 3,
            "id": id
        },
        dataType: "json",
        success: function(e) {
            if (e == 1) {
                alert("删除成功");
                readygrade();
            } else if (e == 0)
                alert("删除失败");
            else if (e == -1)
                alert("服务器故障");
        }
    });
}